package org.jeecg.modules.htxincailiao.versions.controller;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.QrcodeUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.htxincailiao.versions.entity.HTVersions;
import org.jeecg.modules.htxincailiao.versions.service.IHTVersionsService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: ht_versions
 * @Author: jeecg-boot
 * @Date: 2023-03-10 10:06:30
 * @Version: V1.0
 */
@Api(tags = "ht_versions")
@RestController
@RequestMapping("/versions/hTVersions")
@Slf4j
public class HTVersionsController extends JeecgController<HTVersions, IHTVersionsService> {

    //	https://www.htxincailiao.com/apifiles/20230303/%E4%B8%AD%E4%BD%93%E5%88%86%E9%93%9D%E7%A2%B3%E5%8C%96%E7%A1%85%E5%85%89%E5%AD%A6%E7%B3%BB%E7%BB%9F%E9%95%9C%E6%9E%B6_1677811122541.jpg
    private static final String QRCODE_URL_PREFIX = "https://www.htxincailiao.com";
    @Value(value = "${spring.profiles.active}")
    private String springEnv;


    @Value(value = "${server.port}")
    private String serverPort;

    @Value(value = "${server.servlet.context-path}")
    private String serverServletContextPath;

    @Autowired
    private IHTVersionsService hTVersionsService;



    /**
     * 通过id查询
     *
     * @return 最新版本信息
     */
    @AutoLog(value = "ht_versions-获取最新版本信息")
    @ApiOperation(value = "ht_versions-获取最新版本信息", notes = "ht_versions-获取最新版本信息")
    @GetMapping(value = "/getLastVersion")
    public Result<?> getLastVersion() throws IllegalAccessException {
        HTVersions hTVersions = hTVersionsService.queryLastVersion();
        if (hTVersions == null) {
            return Result.error("未找到对应数据");
        }

        String qrcodeUrl = hTVersions.getQrCodeUrl();

        HTVersions v = new HTVersions();
        v.setQrCodeUrl(qrcodeUrl);

        return Result.OK(v);
    }


    /**
     * 分页列表查询
     *
     * @param hTVersions
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "ht_versions-分页列表查询")
    @ApiOperation(value = "ht_versions-分页列表查询", notes = "ht_versions-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(HTVersions hTVersions, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<HTVersions> queryWrapper = QueryGenerator.initQueryWrapper(hTVersions, req.getParameterMap());
        Page<HTVersions> page = new Page<HTVersions>(pageNo, pageSize);
        IPage<HTVersions> pageList = hTVersionsService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param hTVersions
     * @return
     */
    @AutoLog(value = "ht_versions-添加")
    @ApiOperation(value = "ht_versions-添加", notes = "ht_versions-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@Valid @RequestBody HTVersions hTVersions) throws UnknownHostException {
//		http://localhost:8980/api/sys/common/static/files/20230310/app-release_1678431180964.apk

        HTVersions versionsRecord = hTVersionsService.queryByNumber(hTVersions.getNumber());
        if (oConvertUtils.isNotEmpty(versionsRecord)) {
            return Result.error("版本号重复, version_number = "+hTVersions.getNumber());
        }

        String appPackagePath = hTVersions.getAppPackage();
        String qrcodeUrlPrefix = null;
        if (springEnv.equals("prod")) {
            qrcodeUrlPrefix = QRCODE_URL_PREFIX;
        } else {
//            qrcodeUrlPrefix = "http://" + "192.168.3.158" + ":" + serverPort;
            qrcodeUrlPrefix = "http://" + "192.168.11.123" + ":" + serverPort;
        }

        String downloadUrl = qrcodeUrlPrefix + serverServletContextPath + "/sys/common/static/" + appPackagePath;
        String fileName = "android_" + hTVersions.getNumber() + ".png";
        String qrcodeUrl = QrcodeUtil.createQrCodeFile(downloadUrl, fileName);
        hTVersions.setQrCodeUrl(qrcodeUrl);
        if (oConvertUtils.isEmpty(hTVersions.getDownloadUrl())) {
            hTVersions.setDownloadUrl(downloadUrl);
        }

        hTVersionsService.save(hTVersions);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param hTVersions
     * @return
     */
    @AutoLog(value = "ht_versions-编辑")
    @ApiOperation(value = "ht_versions-编辑", notes = "ht_versions-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody HTVersions hTVersions) {
        hTVersionsService.updateById(hTVersions);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ht_versions-通过id删除")
    @ApiOperation(value = "ht_versions-通过id删除", notes = "ht_versions-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        hTVersionsService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "ht_versions-批量删除")
    @ApiOperation(value = "ht_versions-批量删除", notes = "ht_versions-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.hTVersionsService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ht_versions-通过id查询")
    @ApiOperation(value = "ht_versions-通过id查询", notes = "ht_versions-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        HTVersions hTVersions = hTVersionsService.getById(id);
        if (hTVersions == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(hTVersions);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param hTVersions
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HTVersions hTVersions) {
        return super.exportXls(request, hTVersions, HTVersions.class, "ht_versions");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HTVersions.class);
    }

}
